'''
来自吾爱破解

'''

import os
import requests
from lxml import etree

url = "https://www.qidian.com/all"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
sep1 = requests.get(url=url, headers=headers).text
tree = etree.HTML(sep1)
detail_url = "https:" + tree.xpath('//div[@class="all-book-list"]//a[1]/@href')[0]
sep2 = requests.get(url=detail_url, headers=headers).text
tree2 = etree.HTML(sep2)
book_name = tree2.xpath('//div[@class="book-info "]/h1//text()')[0]
li_list = tree2.xpath('//*[@id="j-catalogWrap"]/div[2]/div[1]/ul/li')
if not os.path.exists('./dafeng/'):
    os.mkdir('./dafeng/')

for li in li_list:
    detai_url_page = "https:" + li.xpath('./a/@href')[0]
    detai_url_page_name = li.xpath('./a/text()')[0] + ".txt"
    # print(detai_url_page,detai_url_page_name)
    sep3 = requests.get(url=detai_url_page, headers=headers).text
    tree3 = etree.HTML(sep3)
    page_deatil = tree3.xpath('//div[@class="read-content j_readContent"]//text()')
    path = './dafeng/' + detai_url_page_name
    print(detai_url_page_name, "下载成功")
    for page in page_deatil:
        with open(path, "a", encoding="utf-8") as pf:
            pf.write(page)
print(book_name, "下载完成")